import { BaseWindow } from "../../../../component/window/BaseWindow";
import { PageTool } from "../../../../common/global/PageTool";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { GuildEventName } from "../GuildEventName";
import { Language } from "../../../../../extend/language/Language";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ImagesConst } from "../../../resource/ImagesConst";
import { TilePanel } from "../../equipment/TilePanel";
import { MemberRender } from "./MemberRender";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { AssignBaseItem } from "./AssignBaseItem";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SGuildPlayer } from "../../../../../Message/Game/SGuildPlayer";
import { Cache } from "../../../cache/Cache";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { MsgManager } from "../../../manager/MsgManager";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { SPlayerItem } from "../../../../../Message/Public/SPlayerItem";
import { ItemData } from "../../../resource/info/ItemData";
import { EPlayerItemPosType } from "../../../../../Message/Public/EPlayerItemPosType";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class AssignItemWindow
    
    export  class AssignItemWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._pageTool = new PageTool([], 4);
            this.init();
            this.addEventListener(flash.events.Event.CHANGE, this.onCurPageChangeHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseSendToAssign, this.onWarehouseSendToAssignHandler);
            return;
        }

        public updateMaxGold(arg1: int): void
        {
            this._maxGold = arg1;
            return;
        }

        /* internal  */init(): void
        {
            var loc1=null;
            this.setSize(302, 345);
            this.titleHeight = 28;
            this.title = Language.getString(57249);
            ObjCreate.createBg(17, 40, 267, 192, this);
            ObjCreate.createTextField(Language.getString(57250), 122, 42, 220, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createBg(22, 58, 260, 2, this, ImagesConst.SplitLine);
            ObjCreate.createTextField(Language.getString(57251), 55, 65, 50, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createTextField(Language.getString(57252), 198, 65, 50, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createBg(22, 80, 260, 2, this, ImagesConst.SplitLine);
            this._memListPanel = new TilePanel();
            this._memListPanel.horizontalGap = 2;
            this._memListPanel.verticalGap = 2;
            this._memListPanel.setSize(260, 122);
            this._memListPanel.columnWidth = 260;
            this._memListPanel.rowHeight = 26;
            this._memListPanel.setStyle("cellRenderer", MemberRender);
            this._memListPanel.move(19, 85);
            this.addChild(this._memListPanel);
            this._pageBar = new PageSelecter();
            this._pageBar.mode = PageSelecter.SingleMode;
            this._pageBar.x = 109;
            this._pageBar.y = 210;
            this.addChild(this._pageBar);
            ObjCreate.createBg(17, 235, 267, 97, this);
            this._tipTxt = ObjCreate.createTextField("", 31, 237, 250, 20, this, GlobalStyle.textFormat5);
            this._tipTxt.htmlText = Language.getString(57253);
            var loc2=0;
            while (loc2 < 6) 
            {
                loc1 = new AssignBaseItem();
                loc1.x = 20 + loc2 * 44;
                loc1.y = 256;
                loc1.posIndex = loc2;
                this.addChild(loc1);
                AssignItemWindow._assignItemArray.push(loc1);
                ++loc2;
            }
            ObjCreate.createTextField(Language.getString(57614), 22, 310, 250, 20, this, GlobalStyle.textFormat5);
            this._moneyInput = ObjCreate.createGTextInput(85, 307, 70, 20, this);
            this._moneyInput.text = "0";
            this._moneyInput.restrict = "0-9";
            this._moneyInput.maxChars = 8;
            this._moneyInput.addEventListener(flash.events.Event.CHANGE, this.onMoneyChangeHandler);
            ObjCreate.createBitmap(ImagesConst.Yuanbao, 160, 310, this);
            this._assignBtn = ObjCreate.createGButton(Language.getString(57254), 225, 300, 49, 28, this);
            this._assignBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAssignBtnClickHandler);
            return;
        }

        public setDefaultPage(arg1: Array<any>): void
        {
            var loc1=[];
            if (arg1) 
            {
                this._pageTool.array = this.filterData(arg1);
            }
            this._pageBar.currentPage = 1;
            this._pageBar.maxPage = this._pageTool.pagesCount;
            this.updateItemListProvider(this._pageTool.getByPageNo(1));
            return;
        }

        public updateItemListProvider(arg1: Array<any>): void
        {
            this._memListPanel.dataProvider = new DataProvider(arg1);
            return;
        }

        /* internal  */filterData(arg1: Array<any>): Array<any>
        {
            var loc2=null;
            var loc3=0;
            var loc1=[];
            if (!(arg1 == null) && arg1.length > 0) 
            {
                loc3 = 0;
                while (loc3 < arg1.length) 
                {
                    loc2 = arg1[loc3] as SGuildPlayer;
                    if (Cache.instance.guild.isGuildLeader) 
                    {
                        if (loc2.position != EGuildPosition._EGuildLeader) 
                        {
                            loc1.push(loc2);
                        }
                    }
                    else if (!(loc2.position == EGuildPosition._EGuildLeader) && !(loc2.position == EGuildPosition._EGuildDeputyLeader)) 
                    {
                        loc1.push(loc2);
                    }
                    ++loc3;
                }
            }
            return loc1;
        }

        public resetToDefault(): void
        {
            this._memListPanel.selectedIndex = -1;
            this.clearAssignItems();
            this._moneyInput.text = "0";
            return;
        }

        /* internal  */clearAssignItems(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < AssignItemWindow._assignItemArray.length) 
            {
                loc1 = AssignItemWindow._assignItemArray[loc2];
                loc1.itemData = null;
                ++loc2;
            }
            return;
        }

        /* internal  */onCurPageChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._pageBar.currentPage;
            this.updateItemListProvider(this._pageTool.getByPageNo(loc1));
            return;
        }

        /* internal  */isHadItem(): Boolean
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < AssignItemWindow._assignItemArray.length) 
            {
                loc1 = AssignItemWindow._assignItemArray[loc2];
                if (loc1.itemData) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        /* internal  */getToAssignItemsDict(): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc2=null;
            var loc3=null;
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc4=0;
            while (loc4 < AssignItemWindow._assignItemArray.length) 
            {
                loc2 = AssignItemWindow._assignItemArray[loc4];
                loc3 = loc2.itemData;
                if (loc3) 
                {
                    loc1[loc3.uid] = loc3.itemAmount;
                }
                ++loc4;
            }
            return loc1;
        }

        /* internal  */onMoneyChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this.assignedGold >= this._maxGold) 
            {
                this._moneyInput.text = this._maxGold.toString();
            }
            return;
        }

        /* internal  */onAssignBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var guildPlayer: SGuildPlayer;
            var itemsDict: Map<any, any> /* flash.utils.Dictionary */;
            var moneyMap: Map<any, any> /* flash.utils.Dictionary */;
            var onAssignHandler: Function;

            var loc1;
            guildPlayer = null;
            itemsDict = null;
            moneyMap = null;
            onAssignHandler = null;
            e = arg1;
            onAssignHandler = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseAssign, {"toPlayerId": guildPlayer.miniPlayer.entityId.id, "itemMap": itemsDict, "moneyMap": moneyMap}));
                    Alert.resetToDefault();
                }
                return;
            }
            if (this._memListPanel.selectedIndex == -1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57255));
                return;
            }
            if (this.assignedGold == 0 && !this.isHadItem()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57256));
                return;
            }
            guildPlayer = SGuildPlayer(this._memListPanel.dataProvider.getItemAt(this._memListPanel.selectedIndex));
            itemsDict = this.getToAssignItemsDict();
            if (this.assignedGold > 0) 
            {
                moneyMap = new Map<any, any> /* flash.utils.Dictionary */();
                moneyMap[EPrictUnit._EPriceUnitGold] = this.assignedGold;
            }
            Alert.show(this.getAssignAletTip(), null, Alert.OK | Alert.CANCEL, null, onAssignHandler);
            return;
        }

        /* internal  */onWarehouseSendToAssignHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.data as SPlayerItem;
            if (loc1) 
            {
                loc2 = new ItemData(loc1.itemCode);
                loc2.data.uid = loc1.uid;
                loc2.itemAmount = loc1.itemAmount;
                loc2.posType = EPlayerItemPosType._EPlayerItemPosTypeGuildWarehouse;
                if ((loc3 = AssignItemWindow.getExistAssignItem(loc2)) != null) 
                {
                    loc3.maxAmount = loc1.itemAmount;
                    loc3.confirmAssign(loc2);
                }
                else 
                {
                    if ((loc4 = AssignItemWindow.getFirstAbleAssignItem()) == null) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(57257));
                        return;
                    }
                    loc4.maxAmount = loc1.itemAmount;
                    loc4.confirmAssign(loc2);
                }
            }
            return;
        }

        /* internal  */getAssignAletTip(): string
        {
            var loc1=null;
            var loc3=null;
            var loc4=null;
            var loc2=SGuildPlayer(this._memListPanel.dataProvider.getItemAt(this._memListPanel.selectedIndex));
            if (loc2) 
            {
                loc1 = Language.getStringByParam(57615, loc2.miniPlayer.name);
            }
            var loc5=0;
            while (loc5 < AssignItemWindow._assignItemArray.length) 
            {
                loc3 = AssignItemWindow._assignItemArray[loc5];
                if (loc4 = loc3.itemData) 
                {
                    loc1 = loc1 + (ItemsUtil.getItemName(loc4) + "  *" + loc4.itemAmount + "\n");
                }
                ++loc5;
            }
            if (this.assignedGold > 0) 
            {
                loc1 = loc1 + HTMLUtil.addColor(GameDefConfig.instance.getEPrictUnitName(EPrictUnit._EPriceUnitGold) + "  *" + this.assignedGold, "#FF7902");
            }
            return "<font color=\'#ffffff\'>" + loc1 + "</font>";
        }

        /* internal  */get assignedGold(): int
        {
            return int(this._moneyInput.text);
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.resetToDefault();
            return;
        }

        public static getExistAssignItem(arg1: ItemData): AssignBaseItem
        {
            var loc1=null;
            var loc2=0;
            if (arg1) 
            {
                loc2 = 0;
                while (loc2 < AssignItemWindow._assignItemArray.length) 
                {
                    loc1 = AssignItemWindow._assignItemArray[loc2];
                    if (loc1.itemData && loc1.itemData.uid == arg1.uid) 
                    {
                        return loc1;
                    }
                    ++loc2;
                }
            }
            return null;
        }

        public static getFirstAbleAssignItem(): AssignBaseItem
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < AssignItemWindow._assignItemArray.length) 
            {
                loc1 = AssignItemWindow._assignItemArray[loc2];
                if (loc1.itemData == null) 
                {
                    return loc1;
                }
                ++loc2;
            }
            return null;
        }

        /* internal */ /* var */_titleTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_memListPanel: TilePanel;

        /* internal */ /* var */_pageBar: PageSelecter;

        /* internal */ /* var */_moneyInput: GTextInput;

        /* internal */ /* var */_assignBtn: GButton;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_maxGold: int;

        /* internal */ static /* var */_assignItemArray: Array<any>;
    }

            AssignItemWindow._assignItemArray = [];
        